const app = getApp()
const { dataByIdApi } = require('../../../api/services/yhHidden/index')
const { descApi, pdfApi } = require('../../../api/services/yhList/index')
import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog'

Page({
  data: {
    listData: [],
    params: { comId: '', divideId: '' },
    userType: '', //用户类型
    pcxFlag: false,
    YHZG_Edit: null,
    YHPC_Edit: null,
  },

  //  获取排查单
  async getDescData() {
    const res = await descApi(this.data.params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    this.setData({
      listData: res.rows,
    })
  },

  // 获取排查项
  async getDescDataList(id) {
    let params = { id: id }
    const res = await dataByIdApi(params)
    if (res.code != 200) {
      return app.toast(res.msg)
    }
    let list = res.data.list

    if (list.length == 0) {
      this.setData({ pcxFlag: true })
    } else {
      this.setData({ pcxFlag: false })
    }
  },

  //id_ok =1 确认发送
  async handleSed(event) {
    //  判断是否有排查项
    let _this = this
    let { id } = event.currentTarget.dataset

    if (this.data.pcxFlag) {
      return app.toast('请添加排查项！')
    }
    Dialog.confirm({
      message: '是否发送给企业？',
    })
      .then(async () => {
        await this.getDescDataList(id)
        await _this.sedComData(id)
      })
      .catch(() => {
        app.toast('已取消')
      })
  },

  // 发送给企业接口
  sedComData(id) {
    let _this = this
    const params = 'id=' + id
    wx.request({
      url: app.globalData.host + 'scy/app/spTrouble/sendBill',
      method: 'POST',
      data: params, //参数为键值对字符串
      header: {
        //设置参数内容类型为x-www-form-urlencoded
        'content-type': 'application/x-www-form-urlencoded',
        Accept: 'application/json',
        Authorization: 'Bearer ' + wx.getStorageSync('token'),
      },
      success: function (res) {
        app.toast('发送成功！')
        _this.getDescData()
      },
      error: function (err) {
        let toast = res.data.msg + ''
        app.toast(toast)
      },
    })
  },

  // 添加排查单
  handleAddList() {
    wx.navigateTo({ url: '/pages/admin/yhpcAdd/yhpcAdd' })
  },

  //跳转详情
  handleDesc(event) {
    let { item } = event.currentTarget.dataset
    let id = item.id

    if (this.data.YHZG_Edit) {
      // 可修改
      wx.navigateTo({ url: '/pages/admin/checkDesc/checkDesc?id=' + id })
    } else {
      //只读
      wx.navigateTo({ url: '/pages/admin/dangerList/dangerList?id=' + id })
    }
  },

  //打印排查报告
  async handlePrint(event) {
    let { id } = event.currentTarget.dataset
    let params = { id: id }
    const res = await pdfApi(params)

    let fileName = res.msg
    let host = 'https://dy.microzl.com/pdf/' + fileName
    console.log(host)

    wx.downloadFile({
      url: host, //要预览的PDF的地址
      success: function (res) {
        if (res.statusCode === 200) {
          //成功
          var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
          wx.openDocument({
            filePath: Path, //要打开的文件路径
            success: function (res) {
              console.log('打开PDF成功')
            },
            fail: function (res) {
              console.log('fail')
            },
          })
        }
      },
      fail: function (res) {
        console.log(res) //失败
      },
    })
  },

  onLoad: function (options) {
    console.log(options)

    app.globalData.comId = options.comId
    app.globalData.comName = options.comName

    this.setData({ params: options })

    // 0是企业用户
    // 1是第三方用户（排查人员）
    // 2是管理员（排查人员

    let userInfo = wx.getStorageSync('userInfo')
    let userType = userInfo.userType

    this.setData({ userType: userType })
  },

  onShow: function () {
    this.setData({ YHZG_Edit: wx.getStorageSync('YHZG_Edit'), YHPC_Edit: wx.getStorageSync('YHPC_Edit') })
    this.getDescData()
  },
})
